﻿using System;
using System.Web;
using System.Web.UI;
using Medianamik.Core;
using Medianamik.Core.Profile;
using Medianamik.Core.Security;
using Medianamik.Globalization;
using Telerik.Web.UI;

namespace Medianamik.UI.Web.ModalWindows
{
    public partial class SearchResourceWindow : BasePage
    {
        private string _searchQuery;
        private string SearchQuery
        {
            get
            {
                if (_searchQuery == null)
                {
                    var query = Request.QueryString["search"];
                    if (query != null)
                    {
                        _searchQuery = query.Trim();
                    }
                }
                return _searchQuery;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SearchQuery == null)
            {
                ScriptManager.RegisterStartupScript(this,
                                                    typeof(SearchResourceWindow),
                                                    "CloseWindow",
                                                    "closeWindow('ReloadResourceSets');",
                                                    true);
            }
            else
            {
                Title = Resources("Medianamik_ResourceManagement", "SearchResultsFor") + ": " + SearchQuery;
            }
        }
        protected void RefreshList(object sender, GridNeedDataSourceEventArgs e)
        {
            var manager = new MedianamikResourceDataManager();
            var type = IsCurrentUserSuperAdmin ? ResourceListingTypes.AllResources : ResourceListingTypes.NonSecuredOnly;
            var dt = manager.GetAllResourceIds(SearchQuery, type);
                            
            SearchResults.DataSource = dt;
        }

        protected void LoadResource(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Load")
            {
                ScriptManager.RegisterStartupScript(this,
                                                    typeof(SearchResourceWindow),
                                                    "CloseWindow",
                                                    "closeWindow('" + e.CommandArgument + "');",
                                                     true);
            }
            else
            {
                SearchResults.Rebind();
            }
        }
    }

}
